package com.yft.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamUtils {


	public static void SaveFileFromInputStream(InputStream stream, String path,
			String filename) throws IOException {

		FileOutputStream fs = new FileOutputStream(path + "/" + filename);
		byte[] buffer = new byte[1024 * 1024];
		int bytesum = 0;
		int byteread = 0;
		while ((byteread = stream.read(buffer)) != -1) {
			bytesum += byteread;
			fs.write(buffer, 0, byteread);
			fs.flush();
		}
		fs.close();
		stream.close();
	}



	public static String StreamToString(InputStream in)
	throws IOException
	{
		byte buf[] = new byte[2048];
		StringBuffer sBuf = new StringBuffer();
		int readRes = 0;
		do
		{
			readRes = in.read(buf);
			if(readRes > 0)
				if(readRes < 2048)
				{
					byte destBuf[] = new byte[readRes];
					System.arraycopy(buf, 0, destBuf, 0, readRes);
					sBuf.append(new String(destBuf));
				} else
				{
					sBuf.append(new String(buf));
				}
		} while(readRes > 0);
		return sBuf.toString();
	}

}
